﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Net.Fan.SmallPig.DataAccess;
using System.Data;
using Net.Fan.SmallPig.Common.DBMeta;
using Net.Fan.SmallPig.Common;

namespace Net.Fan.SmallPig.BusinessRules
{
    public class JobBiz
    {
        public static long AddNewJob(long workflowId, string summary, string customer, long count, string description, DateTime? planFinishDate, string createUser)
        {
            string initState = WorkflowDA.GetInitState(workflowId);
            long ji = JobDA.AddNewJob(workflowId, summary, customer, count, description, initState, planFinishDate, createUser);
            DynamicDA.AddNewDynamic(ji, "新建任务", createUser);
            return ji;
        }

        public static DataTable GetWhosOperates(long workflowId, string state, string userId)
        {
            DataTable dt = WorkflowDA.GetWhosOperates(workflowId, state, userId);
            return dt;
        }

        //public static int SetCurrentState(long jobId, string state, string Note)
        //{
        //    JobDA.SetCurrentState(jobId, state);

        //}

        public static void DoOperate(long jobId, long ActiviteId, string note, string user)
        {
            if (JobBiz.HaveActionPermission(ActiviteId, user))
            {
                // WorkflowDA.GetNextState(ActiviteId);
                DataRow dr = WorkflowDA.GetActivity(ActiviteId);
                string nextState = dr[WorkflowMeta.NextState] as string;
                string operation = dr[WorkflowMeta.Operate] as string;

                setState(jobId, note, user, nextState, operation);
            }
        }

        private static void setState(long jobId, string note, string user, string nextState, string operation)
        {
            JobDA.SetCurrentState(jobId, nextState);
            JobDA.LogOperation(jobId, operation, note, user);
            DynamicDA.AddNewDynamic(jobId, operation, user);
        }

        public static void UpdateState_Force(long jobId, string user, string nextState)
        {
            setState(jobId, "强制转换为：" + nextState + " 状态", user, nextState, "强制转换");
        }

        /// <summary>
        /// 是否有当前动作的权限
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool HaveActionPermission(long ActiviteId, string user)
        {

            bool b1 = WorkflowDA.IsActivityOperator(ActiviteId, user);
            bool b2 = GroupDA.IsAdmin(user);
            return b1 || b2;
        }

        public static DataTable SearchJobs(string Keys, JobTimeStatus jobTimeStatus)
        {
            string[] keyArray = Keys.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            return JobDA.SearchJobs(keyArray, jobTimeStatus);
        }

        public static bool IsOverdue(long jobId)
        {
            if (IsFinished(jobId))
            {
                return false;
            }
            else
            {
                DataRow job = JobDA.GetJob(jobId);
                DateTime dt = (DateTime)job[JobMeta.PlanFinishTimeField];
                if (DateTime.Now > dt)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }

        public static bool IsFinished(long jobId)
        {
            DataRow job = JobDA.GetJob(jobId);
            string currentState = job[JobMeta.CurrentStateField] as string;
            long workflowId = (long)job[JobMeta.WorkflowIDField];
            string endState = WorkflowDA.GetEndState(workflowId);
            if (currentState == endState)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void UpdateJob(long jobId, string Summary, DateTime? planFinishTime, int? number, string unit, string customer, string description, string updateUser)
        {
            JobDA.UpdateJob(jobId, Summary, planFinishTime, number, unit, customer, description);
            JobDA.LogOperation(jobId, "更新", "更新了基本信息", updateUser);
        }


    }
}
